public class MethodExercise {
    
    // 判断素数
    public static boolean isPrime(int n) {
        if (n <= 1) return false;
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) return false;
        }
        return true;
    }
    
    // 计算各位数字之和
    public static int digitSum(int n) {
        int sum = 0;
        while (n != 0) {
            sum += n % 10;
            n /= 10;
        }
        return sum;
    }
    
    // 十进制转二进制
    public static String toBinary(int n) {
        if (n == 0) return "0";
        StringBuilder binary = new StringBuilder();
        while (n > 0) {
            binary.insert(0, n % 2);
            n /= 2;
        }
        return binary.toString();
    }
    
    // 方法重载：求和
    public static int sum(int a, int b) {
        return a + b;
    }
    
    public static int sum(int a, int b, int c) {
        return a + b + c;
    }
    
    public static double sum(double a, double b) {
        return a + b;
    }
    
    public static void main(String[] args) {
        System.out.println("17是素数吗? " + isPrime(17));
        System.out.println("123的各位和: " + digitSum(123));
        System.out.println("10的二进制: " + toBinary(10));
        
        System.out.println("两个整数和: " + sum(5, 3));
        System.out.println("三个整数和: " + sum(1, 2, 3));
        System.out.println("两个浮点数和: " + sum(1.5, 2.5));
    }
}
